/* 
 * @Author: 21克的爱情
 * @Date: 2020-09-02 16:19:23
 * @Email: raohong07@163.com
 * @LastEditors: 21克的爱情
 * @LastEditTime: 2020-10-03 22:13:14
 * @Description: 
 */
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import '../resources/ThemeUtils.dart';

class UtilTool {


  // 自定义弹窗带按钮
  static showAlertDialog(context, String content, { String title='', String lefText='取消', String righText='确定', int buttonInt = 0, double btnFontSize=14, double contentFontSize=15, Color contentColor }){
    // 设置按钮的字体颜色默认为主题色
    // Color buttonColor = buttonInt!=0?Color(buttonInt):Theme.of(context).colorScheme;
    
    // 定义按钮的数量
    List<Widget> actions = [
      new CupertinoButton(
        onPressed: () {
          Navigator.of(context).pop(false);
        },
        child: Text(
          lefText,
          style: TextStyle(
            fontSize: btnFontSize,
            color: ThemeUtils.mainTxtColor
          ),
        )
      ),
      new CupertinoButton(
        onPressed: () {
          Navigator.of(context).pop(true);
        },
        child: Text(
          righText,
          style: TextStyle(
            fontSize: btnFontSize,
            color: ThemeUtils.blueColor
          ),
        )
      ),
    ];

    // 根据传递的参数匹配按钮的显示
    if(lefText == null || lefText.isEmpty){
      actions.removeAt(0);
    }
    if(righText == null || righText.isEmpty){
      actions.removeLast();
    }
  
    return showDialog<void>(
      context: context,
      barrierDismissible: false,
      builder: (BuildContext context) {
        return WillPopScope(
          onWillPop: () async => false,
          child: CupertinoAlertDialog(
            title: title != null && title.isNotEmpty ? Text(
              '$title',
            ) : Container(),
            content: Padding(
              padding: EdgeInsets.only(top: title != null && title.isNotEmpty ? 15 : 0),
              child: Text(
                content,
                style: TextStyle(
                  color: contentColor != null ? contentColor : ThemeUtils.mainTxtColor,
                  fontSize: contentFontSize
                ),
              ),
            ),
            actions: actions,
          ),
        );
      }
    );
  }

  // 正方形提示弹窗
  static showToastDialog( String msg, { type: true, bool isDone=true } ){
    Widget custom = type?Center(
      child: Container(
        width: 130,
        height: 130,
        padding: const EdgeInsets.all(10),
        decoration: BoxDecoration(
          color: Colors.black.withOpacity(0.7),
          borderRadius: BorderRadius.all(Radius.circular(5))
        ),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          mainAxisSize: MainAxisSize.min,
          children: <Widget>[
            Icon(
              isDone?Icons.done:Icons.clear,
              color: Colors.white,
              size: 64,
            ),
            Text(
              msg,
              textAlign: TextAlign.center,
              style: TextStyle(color: Colors.white),
            ),
          ],
        ),
      ),
    ):Container(
      padding: const EdgeInsets.symmetric(horizontal: 20, vertical: 8),
      decoration: BoxDecoration(
        color: Colors.black.withOpacity(0.7),
        borderRadius: BorderRadius.all(Radius.circular(5))
      ),
      child: Text(
        msg,
        textAlign: TextAlign.center,
      ),
    );
    showToastWidget(custom, position: type?ToastPosition.center : ToastPosition.bottom);
  }

  static getUriKeyValue(String uri, String key){
    String uriString = uri.indexOf('?') > -1 ? uri.split('?')[1] : uri;
    List<String> urlItem = uriString.split('&');
    Map uriMap = {};
    urlItem.forEach((element) {
      if(element.indexOf('=') > -1){
        List<String> item = element.split('=');
        uriMap[item[0]] = item[1] ?? '';
      }
    });
    return uriMap[key];
  }
}